package com.ldj.struts.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.SystemConfig;
import com.ldj.common.SystemSession;

import com.ldj.hibernate.CaseInfo;
import com.ldj.hibernate.EmployerInfo;
import com.ldj.hibernate.EmployerInfoDAO;
import com.ldj.hibernate.PersionFile;
import com.ldj.hibernate.PersionFileDAO;
import com.ldj.hibernate.PetitionInfo;
import com.ldj.hibernate.PetitionInfoDAO;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;
import com.ldj.struts.form.PetitionInfoForm;

public class PetitionInfoAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			String userId = SystemSession.getSupUserId(request);
			PetitionInfoForm pForm = (PetitionInfoForm)form;
			PetitionInfoDAO dao = new PetitionInfoDAO(request, response);
			List list = dao.findAllPage(userId, pForm);
			request.setAttribute("list", list);
			request.setAttribute("intPage", String.valueOf(dao.intPage));
			return mapping.findForward("list");
	}
	
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
			return mapping.findForward("toAdd");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PetitionInfoForm pForm = (PetitionInfoForm)form;
		PetitionInfo info = new PetitionInfo();
		info.setPetitionInfoRegister(pForm.getPetitionInfoRegister());
		info.setPetitionInfoTime(pForm.getDatePetitionInfoTime());
		info.setPetitionInfoType(pForm.getPetitionInfoType());
		info.setPetitionInfoPersionName(pForm.getPetitionInfoPersionName());
		info.setPetitionInfoTel(pForm.getPetitionInfoTel());
		info.setPetitionInfoAddress(pForm.getPetitionInfoAddress());
		info.setPetitionInfoEmail(pForm.getPetitionInfoEmail());
		EmployerInfoDAO eDao = new EmployerInfoDAO(request, response);
		EmployerInfo eInfo = eDao.findById(pForm.getPetitionInfoReportedUnit());
		info.setEmployerInfo(eInfo);
		info.setPetitionInfoTitle(pForm.getPetitionInfoTitle());
		info.setPetitionInfoContent(pForm.getPetitionInfoContent());
		info.setPetitionInfoDealResult(pForm.getPetitionInfoDealResult());
		info.setPetitionInfoCreateTime(new Date());
		info.setPetitionInfoDelOper("0");
		info.setPetitionInfoIsCase("0");
		info.setPetitionInfoCreator(SystemSession.getSupUserId(request));
		PetitionInfoDAO petitionInfoDao = new PetitionInfoDAO(request, response);
		petitionInfoDao.save(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("0");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "PetitionInfo.add").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getPetitionInfoTitle()));
		logDao.save(s);
		return new ActionForward("/PetitionInfo.do?method=list", true);
	}
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PetitionInfoForm pForm = (PetitionInfoForm)form;
		PetitionInfoDAO petitionInfoDao = new PetitionInfoDAO(request, response);
		PetitionInfo pinfo = petitionInfoDao.findById(pForm.getPetitionInfoId());
		PersionFileDAO dao = new PersionFileDAO(request, response);
		PersionFile persionInfo = dao.findById(pinfo.getPetitionInfoCreator());
		request.setAttribute("persionInfo", persionInfo);
		request.setAttribute("pInfo", pinfo);
		return mapping.findForward("detail");
	}

	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		    PetitionInfoForm pForm = (PetitionInfoForm)form;
			PetitionInfoDAO dao = new PetitionInfoDAO(request, response);
			PetitionInfo info = dao.findById(pForm.getPetitionInfoId());
			if(info.getPetitionInfoIsCase().equals("0")){
			    request.setAttribute("pInfo", info);
			    request.setAttribute("nowPage",request.getParameter("page"));
			    return mapping.findForward("toUpdate");
			}
			return mapping.findForward("petitionBeen");
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PetitionInfoForm pForm = (PetitionInfoForm)form;
		PetitionInfoDAO dao = new PetitionInfoDAO(request, response);
		PetitionInfo info = dao.findById(pForm.getPetitionInfoId());
		if(info.getPetitionInfoIsCase().equals("0")){
			info.setPetitionInfoAddress(pForm.getPetitionInfoAddress());
			info.setPetitionInfoContent(pForm.getPetitionInfoContent());
			info.setPetitionInfoDealResult(pForm.getPetitionInfoDealResult());
			info.setPetitionInfoEmail(pForm.getPetitionInfoEmail());
			info.setPetitionInfoPersionName(pForm.getPetitionInfoPersionName());
			info.setPetitionInfoRegister(pForm.getPetitionInfoRegister());
			EmployerInfoDAO eDao = new EmployerInfoDAO(request, response);
			EmployerInfo eInfo = eDao.findById(pForm.getPetitionInfoReportedUnit());
			info.setEmployerInfo(eInfo);
			info.setPetitionInfoTel(pForm.getPetitionInfoTel());
			info.setPetitionInfoTime(pForm.getDatePetitionInfoTime());
			info.setPetitionInfoTitle(pForm.getPetitionInfoTitle());
			info.setPetitionInfoType(pForm.getPetitionInfoType());
			dao.merge(info);
			
			SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
			SysLogInfo s = new SysLogInfo();
			s.setPersionFile(SystemSession.getSupUserInfo(request));
			s.setSysLogInfoOperDate(new Date());
			s.setSysLogInfoOperType("1");
			s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
			s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "PetitionInfo.update").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getPetitionInfoTitle()));
			logDao.save(s);
			return new ActionForward("/PetitionInfo.do?method=list&pages="+request.getParameter("pages"), true);
		}
		return mapping.findForward("petitionBeen");
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		PetitionInfoDAO dao = new PetitionInfoDAO(request, response);
		String[]str = request.getParameterValues("pid");
		List list = dao.findFromIdArray(str);
		String title="";
		if(list != null && list.size() > 0){
			for(int i = 0; i < list.size(); i ++){
				PetitionInfo info = (PetitionInfo)list.get(i);
				info.setPetitionInfoDelOper("1");
				title+=info.getPetitionInfoTitle()+",";
				dao.merge(info);
			}
		}
		
		if(title.length()>0){
			title=title.substring(0, title.length()-1);
		}
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("2");
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "PetitionInfo.delete").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record",title));
		logDao.save(s);
		return new ActionForward("/PetitionInfo.do?method=list", true);
	}
	
	public ActionForward toCase(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PetitionInfoDAO dao = new PetitionInfoDAO(request, response);
		PetitionInfo info = dao.findById(request.getParameter("pid"));
		if(info.getPetitionInfoIsCase().equals("0")){
			CaseInfo cInfo = new CaseInfo();
			cInfo.setEmployerInfo(info.getEmployerInfo());
			cInfo.setCaseInfoBasiccondiBeginres(info.getPetitionInfoContent());
			cInfo.setCaseInfoSource("005002");
			
			request.setAttribute("cInfo", cInfo);
			info.setPetitionInfoIsCase("1");
			dao.merge(info);
			SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
			SysLogInfo s = new SysLogInfo();
			s.setPersionFile(SystemSession.getSupUserInfo(request));
			s.setSysLogInfoOperDate(new Date());
			s.setSysLogInfoOperType("11");
			s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
			s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "PetitionInfo.case").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", info.getPetitionInfoRegister()));
			logDao.save(s);
			return mapping.findForward("toCase");
		}
		return mapping.findForward("petitionBeen");
	}

}